package com.aat.dxfy.dxyStart;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;

public class AutoMakeCode extends JFrame implements ActionListener {
	// 参数
	private static final long serialVersionUID = 1L;
	private int x = Toolkit.getDefaultToolkit().getScreenSize().width;
	private int y = Toolkit.getDefaultToolkit().getScreenSize().height;
	private int w = x / 2;
	private int h = y / 2;

	// UI
	private LookAndFeelInfo[] look;
	private Font font = new Font("微软雅黑", 10, 15);

	// 组件
	private JPanel jp;
	private JLabel jl1 = new JLabel("1，填写 com/单位名/项目名 例如下");
	private JTextField jtf = new JTextField("com/aat/dizena");
	private JLabel jl2 = new JLabel("2，选择xlss或xlsm文件");
	private JTextField jtf2 = new JTextField("");
	private JButton jbtn = new JButton("选择xlss或xlsm文件");
	private JButton jb2 = new JButton("执行");
	private JButton jb3 = new JButton("关闭");
	//
	private String str1, str2;

	// 变量
	public AutoMakeCode() {
		super("dizenaStart生成代码");
		this.setIconImage(
				Toolkit.getDefaultToolkit().createImage(getClass().getResource("/com/aat/dxfy/dxyStart/logo16.png")));
		// 添加组件
		this.setBounds(w / 2, h / 2, w, h);
		this.setLayout(null);
		jp = new JPanel(null);
		jp.setBounds(0, 0, w, h);
		jp.setBackground(new Color(0XF0, 0XF0, 0xF0));// #F77B6F
		// 1
		jl1.setFont(font);
		jl1.setBounds(w / 4, h / 6 - 50, w / 2, h / 10);
		jp.add(jl1);
		// 1
		jtf.setFont(font);
		jtf.setBounds(w / 4, h / 6, w / 2, h / 10);
		jp.add(jtf);
		// 2
		jl2.setFont(font);
		jl2.setBounds(w / 4, h / 6 + 50, w / 2, h / 10);
		jp.add(jl2);
		// 2
		jtf2.setFont(font);
		jtf2.setBounds(w / 4, h / 6 + 100, w / 2, h / 10);
		jtf2.setEditable(false);
		jp.add(jtf2);
		jbtn.setFont(font);
		jbtn.setBounds(w / 4, h / 6 + 150, 200, h / 10);
		jp.add(jbtn);

		// 3
		jb2.setFont(font);
		jb2.setBounds(w / 4, h / 6 + 200, 100, h / 10);
		jb2.setBackground(new Color(210, 10, 10));
		jp.add(jb2);

		// 3
		jb3.setFont(font);
		jb3.setBounds(w / 4 + 150, h / 6 + 200, 100, h / 10);
		jp.add(jb3);

		//
		jbtn.addActionListener(this);
		jb3.addActionListener(this);
		jb2.addActionListener(this);
		this.add(jp);
		// ///////////
		try {
			look = UIManager.getInstalledLookAndFeels();
			UIManager.setLookAndFeel(look[1].getClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		// this.setUndecorated(true);
		this.setVisible(true);
		this.setResizable(false);// 不放大
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new AutoMakeCode();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jbtn) {
			JFileChooser jfc = new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter("xlss & xlsm ", "xlss", "xlsm");
			jfc.setFileFilter(filter);
			jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			// jfc.showDialog(new JLabel(), "选择");
			int returnVal = jfc.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = jfc.getSelectedFile();
				if (file.isFile()) {
					String fp = file.getAbsolutePath();
					str2 = file.getName();
					str1 = file.getPath();
					if (fp.endsWith("xlss") || fp.endsWith("xlsm")) {
						jtf2.setText(fp);
					} else {
						jtf2.setText("");
					}
				} else {
					jtf2.setText("");
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择模型表文件路径", "提示消息", JOptionPane.INFORMATION_MESSAGE);
			}

		}

		if (e.getSource() == jb2) {
			str1 = jtf2.getText();
			String packagePath = jtf.getText();
			if (packagePath.split("/").length != 3) {
				JOptionPane.showMessageDialog(null, "请填写代码包路径", "提示消息", JOptionPane.INFORMATION_MESSAGE);
			} else {
				if (null == str1 || str1.length() == 0) {
					JOptionPane.showMessageDialog(null, "请选择模型表文件路径", "提示消息", JOptionPane.INFORMATION_MESSAGE);
				} else {
					str1 = str1.substring(0, str1.length() - str2.length());
					String baseDir = str1;
					String xlsmName = str2;
					//

					DizenaStartCodeMake obj = new DizenaStartCodeMake(baseDir, xlsmName, packagePath);
					try {
						obj.getBaseInfo4Xlsm();
					} catch (IOException e1) {
						e1.printStackTrace();
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, "执行完毕！", "提示消息", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			//
		}

		if (e.getSource() == jb3) {
			System.exit(0);
		}
	}

}
